In-class Ex 3

Author

Stephen Tay

Published

September 9, 2024

Modified

September 9, 2024

1.

pacman::p_load(sf, spNetwork, tmap, tidyverse)

2. Loading Data

network <- st_read(dsn="data/geospatial", 
                   layer="Punggol_St")
Reading layer `Punggol_St' from data source 
  `/Users/stephentay/stephentay/ISSS626-Geospatial-Analytics/In-class_Ex/In-class_Ex03/data/geospatial' 
  using driver `ESRI Shapefile'
Simple feature collection with 2642 features and 2 fields
Geometry type: LINESTRING
Dimension:     XY
Bounding box:  xmin: 34038.56 ymin: 40941.11 xmax: 38882.85 ymax: 44801.27
Projected CRS: SVY21 / Singapore TM

2.2 Punggol Childcare Centers

Childcare point geometries are in 3-dimensional coordinates, they must be transformed to 2-dimensional coordinates using the st_zm() method.

childcare
Simple feature collection with 61 features and 1 field
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 34423.98 ymin: 41503.6 xmax: 37619.47 ymax: 44685.77
Projected CRS: SVY21 / Singapore TM
First 10 features:
      Name                  geometry
1   kml_10 POINT (36173.81 42550.33)
2   kml_99 POINT (36479.56 42405.21)
3  kml_100 POINT (36618.72 41989.13)
4  kml_101 POINT (36285.37 42261.42)
5  kml_122  POINT (35414.54 42625.1)
6  kml_161 POINT (36545.16 42580.09)
7  kml_172 POINT (35289.44 44083.57)
8  kml_188 POINT (36520.56 42844.74)
9  kml_205  POINT (36924.01 41503.6)
10 kml_222 POINT (37141.76 42326.36)

3. Plotting

Plot the road network first, followed by the points. add = TRUE adds the point layer on the road network.

plot(st_geometry(network))
plot(childcare,add=T,col='red',pch = 19)

If you just plot(network), it will plot every column in the network sf dataset, which is meaningless. Thus, the correct way is to use plot(st_geometry(network)) as above.

plot(network)
plot(childcare,add=T,col='red',pch = 19)

tmap_mode('plot') will just plot a static map. Specify the layer to use using tm_shape(). tm_symbols() and tm_markers() when you want to customise the display of the points using png files. Use tm_square(), tm_bubbles(), tm_dots for standard display of points.

tmap_mode('plot')
tm_shape(childcare) + 
  tm_dots(col = 'red') + 
  tm_shape(network) +
  tm_lines()

tmap_mode('view') will plot an interactive map.

tmap_mode('view')
tm_shape(childcare) + 
  tm_dots() + 
  tm_shape(network) +
  tm_lines()
tmap_mode('plot')

4. Network Kernel Density Estimation (NKDE) Analysis

4.1 Prepare lixels

700m was chosen as the lixel length because it’s based on a research on the walkable distance in Singapore environment. Minimum distance was arbitrarily set at 350m.

lixels <- lixelize_lines(network, 
                         lx_length = 700, 
                         mindist = 350)
nrow(lixels)
[1] 2645
lixels <- lixelize_lines(network, 
                         lx_length = 700, 
                         mindist = 50)
nrow(lixels)
[1] 2648

length of distance should not be shorter than the distance between data points. use nearest neighbor method and test out 10th, 25th percentile of the distance between points.

lixels <- lixelize_lines(network, 
                         lx_length = 1000, 
                         mindist = 150)
nrow(lixels)
[1] 2645
samples <- lines_center(lixels) 
tmap_mode('view')
tm_shape(lixels) + 
  tm_lines() + 
  tm_shape(samples) +
  tm_dots(size = 0.01)
tmap_mode('plot')
simple_densities <- nkde(lines = network, events = childcare,
                  w = rep(1, nrow(childcare)), samples = samples,
                  kernel_name = "quartic", bw = 300, 
                  div= "bw", method = "simple", 
                  digits = 1, tol = 1,
                  grid_shape = c(1,1), max_depth = 8,
                  agg = 5, sparse = TRUE,
                  verbose = FALSE)
# number per kilometer
samples$simple_density <- simple_densities*1000
lixels$simple_density <- simple_densities*1000
tmap_mode('view')
tm_shape(lixels) +
  tm_lines(col="simple_density") +
  tm_shape(childcare) +
  tm_dots()
tmap_mode('plot')

5. Network-constrained G- and K-function analysis

A test for complete spatial randomness (CSR) can be performed using the network-constrained G- and K-functions.

The test hypotheses are as follows:

  • H0: The distribution of childcare services in Punggol are randomly distributed along the road network.
  • H1: The distribution of childcare services in Punggol are not randomly distributed along the road network.

Alpha is set at 0.05, with 1000 Monte-carlo simulations.

set.seed(2024)
kfun_childcare <- kfunctions(network, 
                             childcare,
                             start = 0, 
                             end = 1000, 
                             step = 50, 
                             width = 50, 
                             nsim = 1000, 
                             resolution = 50,
                             verbose = FALSE, 
                             conf_int = 0.05)

5.1 Network K-function

The blue line represents the empirical network K-function of the childcare centers in Punggol, while the gray area shows the results of 1,000 simulations within the 2.5% - 97.5% confidence interval. Since the blue line falls below the lower boundary of the envelope between the 200-400m distance range, it indicates a regular pattern of childcare center locations along the Punggol road network. The childcare centers are regularly spaced between 200-400m.

kfun_childcare$plotk

kfun_childcare$plotg